package com.yanyilu.basing.controller;


import com.yanyilu.basing.common.R;
import com.yanyilu.basing.common.ResultCode;
import com.yanyilu.basing.entity.AdminUser;
import com.yanyilu.basing.jwt.JwtIgnore;
import com.yanyilu.basing.service.IAdminUserService;
import com.yanyilu.basing.util.ParseJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sxx
 * @since 2020-10-27
 */
@CrossOrigin
@RestController
@RequestMapping("/adminuser")
public class AdminUserController {
    @Autowired
    IAdminUserService iAdminUserService;


    @JwtIgnore
    @RequestMapping("/admin/login")
    public R adminLogin(@RequestBody AdminUser adminUser){
        return iAdminUserService.login(adminUser);
    }

    /**
     * 添加管理员
     * @param adminUser
     * @return
     */
//    @JwtIgnore
    @RequestMapping("/admin/add")
    public R adminAdd(@RequestBody AdminUser adminUser){
        String role = ParseJwtUtil.getRole();
        if(role.equals("0")){
            return iAdminUserService.adminAdd(adminUser);
        }else {
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }
    }

    @RequestMapping("/admin/delete")
    public R adminDelete(@RequestParam("adminUserId") Integer adminUserId){
        String role = ParseJwtUtil.getRole();
        if(role.equals("0")){
            return iAdminUserService.adminDelete(adminUserId);
        }else {
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }
    }

    @RequestMapping("/admin/update")
    public R adminUpdate(@RequestBody AdminUser adminUser){
        String role = ParseJwtUtil.getRole();
        if(role.equals("0")){
            return iAdminUserService.adminUpdate(adminUser);
        }else {
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 查询管理员列表
     * @param adminUserName
     * @return
     */
    @RequestMapping("/admin/select")
    public R adminSelect(@RequestParam(value="adminUserName",required = false) String adminUserName,
                         @RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        if(null!=adminUserName){
            return iAdminUserService.adminSelectList(page,limit,adminUserName);
        }else {
            return iAdminUserService.adminSelectList(page,limit);
        }
    }

    @JwtIgnore
    @RequestMapping("/select")
    public R Select(@RequestParam("page")Integer page,
                    @RequestParam("tableName")String tableName){

        return iAdminUserService.selectByAllTableContent(page,tableName);
    }

    @JwtIgnore
    @RequestMapping("/delete")
    public R Delete(@RequestParam("tableName")String tableName,
                    @RequestParam("tableNameIdWord")String tableNameIdWord,
                    @RequestParam("tableNameId")Integer tableNameId){

        return iAdminUserService.deleteByAllId(tableName,tableNameIdWord,tableNameId);
    }
}
